# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
 -------------------------------------------------
    File Name:     variable_management.py
    Description:   变量管理
 -------------------------------------------------
 """

from core.caselog import CaseRunLog


class VariableManager(dict, CaseRunLog):
    def __init__(self, parent=None):
        """
        初始化变量管理器
        :param parent: 支持继承父环境
        """
        super().__init__(parent if parent is not None else dict())

    def get(self, name):
        """获取变量值，如果当前环境没有，则向父环境查询"""
        if name in self:
            return self[name]
        else:
            raise ValueError("Variable %s is undefined" % name)

    def set(self, key, value):
        """设置变量值"""
        # print("设置全局变量: ", key, value)

        super().__setitem__(key, value)
        self.info_log(f"已设置环境变量【{key}】, 值为 【{value}】")

    def unset(self, key):
        """删除变量"""
        super().__delitem__(key)

    def has(self, name):
        """判断是否存在"""
        return name in self


class Globals(VariableManager):
    pass


class Environment(VariableManager):
    def __init__(self, parent=None):
        super().__init__(parent)


class Variables(VariableManager):
    pass


if __name__ == '__main__':
    var = Variables({"global": "test"})
    var.set('name', 'zhangsan')
    print(var)
